package com.person.webdemoproject2.service;
import com.person.webdemoproject2.mapper.PostMapper;
import com.person.webdemoproject2.pojo.Collections;
import com.person.webdemoproject2.pojo.Like_statue;
import com.person.webdemoproject2.pojo.Post;
import com.person.webdemoproject2.utils.jwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.person.webdemoproject2.utils.sendNoticeUtils;
import java.time.LocalDateTime;

@Service
public class PostServiceimpl implements PostService {
    @Autowired
    PostMapper postMapper;
    @Autowired
    sendNoticeUtils sendNoticeUtils;


    @Override
    public boolean likePost(int postId,String token) {
        Integer userId = jwtUtils.getUserId(token);
        Integer i = postMapper.check_like(postId, userId);
        if (i==0){
            Like_statue like_statue = new Like_statue();

            like_statue.setUserid(userId);
            like_statue.setOtherid(postId);
            like_statue.setStatue(1);
            like_statue.setCreate_time(LocalDateTime.now());
            like_statue.setUpdate_time(LocalDateTime.now());
            postMapper.create_like(like_statue);
            postMapper.likePost(postId);
            String content="有用户点赞了您的帖子";
            Integer poster=postMapper.findSender(postId);
            String  type="Like";
            sendNoticeUtils.sendNotification(poster,userId,type,content);
            return true;
        }
        i=postMapper.check_statue(postId,userId);
        if (i==0){
            LocalDateTime now = LocalDateTime.now();
            Integer statue = 1;
            postMapper.updateStatue(postId,userId,statue,now);
            postMapper.likePost(postId);
            return true;
        }
            LocalDateTime now = LocalDateTime.now();
            Integer statue = 0;
            postMapper.updateStatue(postId,userId,statue,now);
            postMapper._likePost(postId);
        return false;
    }

    @Override
    public int getLikeCount(int postId) {
      return postMapper.getLikeCount(postId);
    }

    @Override
    public boolean collectPost(Collections collections, String token) {


        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = com.person.webdemoproject2.utils.jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        collections.setUserid(userid);
        LocalDateTime now = LocalDateTime.now();
        collections.setCreate_time(now);
        collections.setUpdate_time(now);
        if (postMapper.checkcollect(userid,collections.getPostid())==1){
            return false;
        }
        postMapper.collectPost(collections);
        return true;
    }

    @Override
    public boolean likeComment(Integer commentId, String token) {
        Integer userId = jwtUtils.getUserId(token);
        Integer a = postMapper.check_clike(commentId, userId);
        if (a==0){
            Like_statue like_statue = new Like_statue();

            like_statue.setUserid(userId);
            like_statue.setOtherid(commentId);
            like_statue.setStatue(1);
            like_statue.setCreate_time(LocalDateTime.now());
            like_statue.setUpdate_time(LocalDateTime.now());
            postMapper.create_likec(like_statue);
            postMapper.likeComment(commentId);
            Integer user = postMapper.findCommenter(commentId);
            String type="Like";
            String content="有用户点赞了你的评论";
            sendNoticeUtils.sendNotification(user,userId,type,content);
            return true;
        }
        a=postMapper.check_cstatue(commentId,userId);
        if (a==0){
            LocalDateTime now = LocalDateTime.now();
            Integer statue = 1;
            postMapper.updatecStatue(commentId,userId,statue,now);
            postMapper.likeComment(commentId);
            return true;
        }else {
            LocalDateTime now = LocalDateTime.now();
            Integer statue = 0;
            postMapper.updatecStatue(commentId,userId,statue,now);
            postMapper._likeComment(commentId);
        }
        return false;
    }

    @Override
    public int getLikecomment(Integer commentId) {
        return postMapper.getLikeComment(commentId);
    }

    @Override
    public boolean updatePost(Post post, String token) {
        Integer userId = jwtUtils.getUserId(token);
        Integer postId = post.getId();

        Integer check_u = postMapper.check_user(userId,postId);

        if (check_u==1){
            post.setUpdate_time(LocalDateTime.now());
            postMapper.updatePost(post);
            return true;
        }
        return false;
    }
}
